package edu.scut.course_management.handler;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import edu.scut.course_management.result.JsonResult;
import edu.scut.course_management.result.JsonResultFactory;
import edu.scut.course_management.result.ResultCode;
import edu.scut.course_management.util.ResponseWritingUtil;

/**
 * 匿名访问处理器 未登录用户访问资源的时候的处理逻辑，返回 json 数据给前端，提示 “用户未登录”
 */
@Component
public class AnonymousAccessHandler implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        JsonResult result = JsonResultFactory.getInstance().buildFailResult(ResultCode.USER_NOT_LOGIN);
        ResponseWritingUtil.writeJsonResultToResponse(response, result);
    }
}
